<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="../../css/bootstrap-table.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单，包含了 popper.min.js -->
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="../../js/bootstrap.min.js"></script>
    <script src="../../js/bootstrap-table.min.js"></script>
    <script src="../../js/bootstrap-table-zh-CN.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>

<table id="tab">

</table>

<!-- 1级模态框-->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">生产派工</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="container"
                     style="padding-bottom:10px ;box-shadow: rgba(144,146,154,0.46) 5px 5px; background-color: whitesmoke;text-align: center">
                    <div>
                        <h2>生产派工单</h2>
                    </div>
                    <div class="">
                        <form>
                            <div class="form-row">
                                <div class="col">
                                    派工单编号： <span id="manufactureId"></span>
                                </div>
                                <div class="col"> &nbsp;
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col">
                                    产品编号： <span id="productId"></span>
                                </div>
                                <div class="col">
                                    产品名称:<span id="productName"></span>
                                </div>
                            </div>

                            <div class="form-row">
                                <div class="col">
                                    描述： <span id="describe"></span>
                                </div>
                                <div class="col">
                                    数量:
                                    <span id="quantity"></span>
                                </div>
                            </div>
                            <table id="table1" style="width: 100%; margin-top: 20px">
                                <thead>
                                <tr style="background-color: #a4efef;">
                                    <th>工序名称</th>
                                    <th>设计工时数</th>
                                    <th>实际工时数</th>
                                    <th>设计工时成本</th>
                                    <th>实际工时成本</th>
                                    <th>设计物料成本</th>
                                    <th>实际物料成本</th>
                                    <th>工序登记</th>
                                    <th>工序交接</th>
                                </tr>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                            <div class="form-row">
                                <div class="col">
                                    设计工时总成本： <span id="labour_cost_price_sum"></span>
                                </div>
                                <div class="col">
                                    设计物料总成本:
                                    <span id="module_cost_price_sum"></span>
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col">
                                    实际工时总成本： <span id="real_labour_cost_price_sum"></span>
                                </div>
                                <div class="col">
                                    实际物料总成本:
                                    <span id="real_module_cost_price_sum"></span>
                                </div>
                            </div>

                            <div class="form-row">
                                <div class="col">
                                    登记人： <span id="registrant1"></span>
                                </div>
                                <div class="col">
                                    登记时间:
                                    <span id="registrationTime1"></span>
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col">
                                    复核人： <span id="checker"></span>
                                </div>
                                <div class="col">
                                    复核时间:
                                    <span id="check_time"></span>
                                </div>
                            </div>
                        </form>
                        <div style="margin: 20px 0px">
                            备注：
                            <textarea class="form-control" id="remark1" readonly></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="exampleModal1" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel1">生产登记单</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="container"
                     style="padding-bottom:10px ;box-shadow: rgba(144,146,154,0.46) 5px 5px; background-color: whitesmoke;text-align: center">
                    <div>
                        <h2>生产登记单</h2>
                    </div>
                    <div class="">
                        <form>
                            <div class="form-row">
                                <div class="col">
                                    派工单编号： <span id="manufactureId1"></span>
                                    <input type="hidden" id="id">
                                </div>
                                <div class="col">
                                    工序名称： <span id="procedureName"></span>
                                    <input type="hidden" id="procedureId">
                                    <input type="hidden" id="cost_price">
                                    <input type="hidden" id="product_id">
                                    <input type="hidden" id="parentId2">
                                    <input type="hidden" id="id2">
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col">
                                    负责人:<input id="principal" type="text" class="form-control"/>
                                </div>
                                <div class="col">
                                    设计工时数:<span id="labourHourAmount"></span>
                                </div>
                            </div>

                            <div class="form-row">
                                <div class="col">
                                    &nbsp;
                                </div>
                                <div class="col">
                                    本次工时数:
                                    <input id="realLabourHourAmount" type="text" class="form-control"/>
                                </div>
                            </div>
                            <table id="table2" style="width: 100%; margin-top: 20px">
                                <thead>
                                <tr style="background-color: #a4efef;">
                                    <th>序号</th>
                                    <th>物料名称</th>
                                    <th>物料编号</th>
                                    <th>设计数量</th>
                                    <th>单位成本</th>
                                    <th>已用数量</th>
                                    <th>本次数量</th>
                                </tr>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                            <div class="form-row" style="margin:50px 0px">
                                <div class="col">
                                    登记人： <span id="registrant2"></span>
                                </div>
                                <div class="col">
                                    登记时间:
                                    <span id="registrationTime2"></span>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button id="submitApplications" type="button" onclick="submitApplications()"
                        class="btn btn-primary">提交
                </button>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="exampleModal3" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel5">工序交接</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body" style="height: 500px">
                <div class="input-group mb-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="inputGroup-sizing-default">本工序合格数量</span>
                    </div>
                    <input type="number" class="form-control" aria-label="Sizing example input"
                           aria-describedby="inputGroup-sizing-default" id="number1">
                    <input type="hidden" id="number2">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="SubmitProcessHandover()">提交
                </button>
            </div>
        </div>
    </div>
</div>

<script>
    var falg = true;


    function submitApplications() {
        $("#table2 tbody tr").each(function (i, n) {
            if (parseInt($(n).find("td input").val()) != parseInt($(n).find("td").eq(3).text())) {
                alert("物料需要用完")
                falg = false;
            } else {
                falg = true;
            }
        })

        if (falg) {
            let array = new Array();
            var sum = 0;
            $("#table2 tbody tr").each(function (i, n) {
                var procedureModulingObj = {
                    id: $(n).find("td").eq(0).text,
                    productId: $(n).find("td").eq(2).text(),
                    productName: $(n).find("td").eq(1).text(),
                    costPrice: $(n).find("td").eq(4).text(),
                    amount: $(n).find("input").eq(0).val(),
                    subtotal: (parseInt($(n).find("td").eq(4).text()) * parseInt($(n).find("input").eq(0).val()))
                }
                sum += procedureModulingObj.subtotal;
                console.log(sum)
                console.log(procedureModulingObj.subtotal)
                array.push(procedureModulingObj)
            })
            var proceduringObj = {
                id: $("#product_id").val(),
                parentId: $("#id").val(),
                procedureId: $("#procedureId").val(),
                procedureName: $("#procedureName").text(),
                labourHourAmount: $("#realLabourHourAmount").val(),
                costPrice: $("#cost_price").text(),
                subtotal: (parseInt($("#cost_price").text()) * parseInt($("#realLabourHourAmount").val())),
                procedureResponsiblePerson: $("#principal").val(),
                register: $("#registrant2").text(),
                registerTime: $("#registrationTime2").text(),
                manufactureId: $("#manufactureId1").text(),
                realModuleSubtotal: sum,
                procedureModulingObj: array
            }

            $.ajax({
                type: "post",
                data: JSON.stringify(proceduringObj),
                url: "/InternalScheduling/submitApplications",
                contentType: "application/json",
                success: function (data) {
                    if (data > 0) {
                        alert("登记成功等待审核")
                        $("#tab").bootstrapTable("refresh");
                        $("#exampleModal").modal("hide");
                        $("#exampleModal1").modal("hide");
                    } else {
                        alert("登记失败")
                    }

                }
            })
        }
    }

    $(function () {
        $("#tab").bootstrapTable({
            method: "post",
            contentType: "application/json",
            striped: true,
            pageNumber: 1,
            pageSize: 5,
            pagination: true,
            sidePagination: 'server',
            pageList: [1, 3, 5, 10],
            url: "/InternalScheduling/QueryRegister",
            queryParams: function (params) {
                return {
                    // 计算页号(当前页)
                    pageNumber: (params.offset / params.limit) + 1,
                    // 计算页大小
                    pageSize: params.limit
                }
            },
            columns: [
                {title: '派序号编号', field: 'manufactureId', align: 'center',},
                {title: '产品编号', field: 'productId', align: 'center',},
                {title: '产品名称', field: 'productName', align: 'center',},
                {title: '数量', field: "amount", align: 'center'},
                {title: '登记时间', field: "registerTime", align: 'center'},
                {
                    title: '登记', field: 'on',
                    formatter: function (value, row, index) {
                        return "<button class='btn btn-primary'  data-toggle=\"modal\"  data-target=\"#exampleModal\" onclick='registerDispatching (" + row.manufactureId + ")' >登记</button>";
                    }

                }
            ]
        })
    })


    function registerDispatching(id) {
        $.ajax({
            type: "post",
            url: "/InternalScheduling/registerDispatching",
            data: "id=" + id,
            success: function (data) {
                $("#id").val(data.id);
                $("#manufactureId").text(data.manufactureId);
                $("#productId").text(data.productId);
                $("#productName").text(data.productName);
                $("#describe").text(data.productDescribe);
                $("#quantity").text(data.amount);
                $("#module_cost_price_sum").text(data.moduleCostPriceSum);
                $("#labour_cost_price_sum").text(data.labourCostPriceSum);
                $("#registrant1").text(data.register);
                $("#registrationTime1").text(data.registerTime);
                $("#checker").text(data.checker);
                $("#check_time").text(data.checkTime);
                let procedureList = data.procedureList;
                $("#table1 tbody").html("");
                for (let i = 0; i < procedureList.length; i++) {
                    $("#table1 tbody").append("<tr>" +
                        "<td>" + procedureList[i].procedureName + "</td>" +
                        "<td>" + procedureList[i].labourHourAmount + "</td>" +
                        "<td>" + (procedureList[i].realLabourHourAmount == null ? '' : procedureList[i].realLabourHourAmount) + "</td>" +
                        "<td>" + (procedureList[i].subtotal == null ? '' : procedureList[i].subtotal) + "</td>" +
                        "<td>" + (procedureList[i].realSubtotal == null ? '' : procedureList[i].realSubtotal) + "</td>" +
                        "<td>" + (procedureList[i].moduleSubtotal == null ? '' : procedureList[i].moduleSubtotal) + "</td>" +
                        "<td>" + (procedureList[i].realModuleSubtotal == null ? '' : procedureList[i].realModuleSubtotal) + "</td>" +
                        "<td>" + (procedureList[i].procedureFinishTag == 'g004-0' ?
                            '<a href="#" class=\"btn btn-outline-success \" data-target=\"#exampleModal1\" onclick="checkIn(' + procedureList[i].id + "," + procedureList[i].detailsNumber +',this)">登记</a>' : (procedureList[i].procedureFinishTag == 'g004-1' ? '待审核' : '已完成')) + "</td>" +
                        "<td>" + (procedureList[i].procedureTransferTag == 'g005-0' ?
                            (procedureList[i].procedureFinishTag == 'g004-3' ?
                                    '<a href="#" class=\"btn btn-outline-warning\" onclick="TheHandoverProcess('+"'"+procedureList[i].procedureId + "'," +procedureList[i].parentId + "," + procedureList[i].detailsNumber +')"   data-target=\"#exampleModal3\">交接工序</a>' : ''
                            ) :
                            (procedureList[i].procedureTransferTag == 'g005-1' ? '等待审核' : '已完成')) + "</td>" +
                        "</tr>");
                }
                var realModuleCostPriceSum=0;
                var realLabourCostPriceSum=0;
                $("#table1 tbody tr").each(function (i,n){
                    realLabourCostPriceSum+=parseInt($(n).find("td").eq(4).text());
                    realModuleCostPriceSum+=parseInt($(n).find("td").eq(6).text());
                })
                $("#real_module_cost_price_sum").text(realModuleCostPriceSum);
                $("#real_labour_cost_price_sum").text(realLabourCostPriceSum);
            }

        })
    }

    function checkIn(id, num,ss) {
        if (num === 1) {
            $.ajax({
                data: "id=" + id,
                type: "post",
                url: "/InternalScheduling/checkInQueryProcedure",
                success: function (data) {
                    $("#product_id").val(id);
                    $("#cost_price").text(data.costPrice);
                    $("#manufactureId1").text($("#manufactureId").text());
                    $("#procedureName").text(data.procedureName);
                    $("#procedureId").val(data.procedureId);
                    $("#labourHourAmount").text(data.labourHourAmount);
                    $("#registrant2").text($("#registrant1").text());
                    $("#registrationTime2").text($("#registrationTime1").text());
                    $("#table2 tbody").html("")
                    for (let i = 0; i < data.procedureModuleList.length; i++) {
                        let list = data.procedureModuleList[i];
                        $("#table2 tbody").append("<tr>" +
                            "<td>" + list.id + "</td>" +
                            "<td>" + list.productName + "</td>" +
                            "<td>" + list.productId + "</td>" +
                            "<td>" + list.amount + "</td>" +
                            "<td>" + list.costPrice + "</td>" +
                            "<td>" + (list.realAmount == null ? '' : list.realAmount) + "</td>" +
                            "<td><input type='number' style='border: none'></td>" +
                            "</tr>")
                    }
                    $("#exampleModal1").modal("show");
                }
            })
        } else {
            if ($(ss).parent().parent().prev().find("td").eq(8).text() != '已完成') {
                alert("上一道工序未完成")
            } else {
                $.ajax({
                    data: "id=" + id,
                    type: "post",
                    url: "/InternalScheduling/checkInQueryProcedure",
                    success: function (data) {
                        $("#product_id").val(id);
                        $("#cost_price").text(data.costPrice);
                        $("#manufactureId1").text($("#manufactureId").text());
                        $("#procedureName").text(data.procedureName);
                        $("#procedureId").val(data.procedureId);
                        $("#labourHourAmount").text(data.labourHourAmount);
                        $("#registrant2").text($("#registrant1").text());
                        $("#registrationTime2").text($("#registrationTime1").text());
                        $("#table2 tbody").html("")
                        for (let i = 0; i < data.procedureModuleList.length; i++) {
                            let list = data.procedureModuleList[i];
                            $("#table2 tbody").append("<tr>" +
                                "<td>" + list.id + "</td>" +
                                "<td>" + list.productName + "</td>" +
                                "<td>" + list.productId + "</td>" +
                                "<td>" + list.amount + "</td>" +
                                "<td>" + list.costPrice + "</td>" +
                                "<td>" + (list.realAmount == null ? '' : list.realAmount) + "</td>" +
                                "<td><input type='number' style='border: none'></td>" +
                                "</tr>")
                        }
                        $("#exampleModal1").modal("show");
                    }
                })
            }
        }
    }

    function TheHandoverProcess(id,parentId,detailsNumber) {
        $("#number2").val(detailsNumber);
        $("#id2").val(id);
        $("#parentId2").val(parentId);
        $("#exampleModal3").modal("show");
    }

    function SubmitProcessHandover() {
        if ( parseInt($("#number2").val()) === 1) {
            if (parseInt($("#number1").val()) > parseInt($("#quantity").text())) {
                alert("合格数量不能大于产品数量");
            } else {
                $.ajax({
                    type:"post",
                    url:"/InternalScheduling/SubmitProcessHandover",
                    data:"manufactureId="+$("#manufactureId").text()+
                        "&id="+$("#id2").val()+"&parentId="+$("#parentId2").val()+
                        "&number="+$("#number1").val(),
                    success:function (data){
                        if (data>0){
                            alert("登记成功")
                            $("#tab").bootstrapTable("refresh");
                            $("#exampleModal").modal("hide");
                            $("#exampleModal1").modal("hide");
                            $("#exampleModal3").modal("hide");
                            localStorage.setItem("sitename",$("#number1").val())
                        }else{
                            alert("登记失败")
                        }
                    }
                })
            }
        } else {
            console.log(localStorage.getItem("sitename"))
            if (parseInt($("#number1").val()) > parseInt(localStorage.getItem("sitename"))) {
                alert("合格数量不能大于上一道工序数量");
            } else {
                $.ajax({
                    type:"post",
                    url:"/InternalScheduling/SubmitProcessHandover",
                    data:"manufactureId="+$("#manufactureId").text()+
                        "&id="+$("#id2").val()+"&parentId="+$("#parentId2").val()+
                        "&number="+$("#number1").val(),
                    success:function (data){
                        if (data>0){
                            alert("登记成功")
                            $("#tab").bootstrapTable("refresh");
                            $("#exampleModal").modal("hide");
                            $("#exampleModal1").modal("hide");
                            $("#exampleModal3").modal("hide");
                        }else{
                            alert("登记失败")
                        }
                    }
                })
            }
        }

    }


</script>

</body>
</html>